package com.jun.controller;

import com.jun.entity.*;
import com.jun.service.ConsumeService;
import com.jun.service.MemberConsumeService;
import com.jun.service.OperationService;
import com.jun.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("user") //会员的控制层
@Slf4j
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    OperationService operationService;
    @Autowired
    MemberConsumeService memberConsumeService;
    @Autowired
    ConsumeService consumeService;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostMapping("/common") //共有查看信息
    public Map<String,Object> commonMessage(){
        HashMap<String, Object> hashMap = new HashMap<>();
        Admin admin = (Admin) SecurityUtils.getSubject().getSession().getAttribute("admin");    //得到登录的用户
        if(admin == null){
            hashMap.put("state",false);
            return hashMap;
        }
        hashMap.put("state",true);
        hashMap.put("admin",admin);
        log.info("查看登录用户的信息");
        return hashMap;
    }

    @PostMapping("/add")    //添加会员
    public Map<String,Object> add(@RequestBody User user){
        HashMap<String, Object> hashMap = new HashMap<>();
        String time = simpleDateFormat.format(new Date());
        String username = user.getUsername();
        String sex = user.getSex();
        String tellphone = user.getTellphone();
        String type = user.getType();
        String consume = Double.parseDouble(user.getConsume())+"";
        String invest = Double.parseDouble(user.getInvest())+"";
        String allConsume = Double.parseDouble(consume) +Double.parseDouble( invest)+"";
        int b = userService.insertUser(new User(user.getId(),username,sex,tellphone,
                time,type,consume,user.getHaircut(),user.getPerm(),user.getWash_protect(),
                user.getHairroot(),user.getColour(),user.getWash(),user.getBlack(),invest,user.getRemarks(),time));   //插入一个会员
        if(b == 1){
            String id = userService.getUserByTellPhone(user.getTellphone()).getId();
            //插入会员消费表
            int i = memberConsumeService.insertMemberConsume(new MemberConsume(id,allConsume , time));
            if(i == 1){
                log.info("会员注册首次消费金额插入！");
            }else{
                log.info("会员注册首次消费金额插入失败！");
            }
            int d = consumeService.insertConsume(new Consume("0", allConsume, time, "会员首次充值和消费"));
            if(d==1){
                log.info("插入一条总消费记录成功！");
            }else{
                log.info("插入一条总消费记录失败！");
            }
            int c = operationService.insertOperation(new Operation(id,username,sex,tellphone,type,
                    "办卡充值"+user.getInvest()+",额外消费"+user.getConsume(),time,time));
            if(c==1){
                log.info("会员办卡充值消费操作记录插入！");
            }else{
                log.info("会员办卡充值消费操作记录插入失败！");
            }
            hashMap.put("state",true);
            hashMap.put("msg","提交成功！");
        }else{
            hashMap.put("state",false);
            hashMap.put("msg","提交失败！");
        }
        return hashMap;
    }

    @GetMapping("/getAllOperation") //获取所有会员操作记录
    public Map<String,Object> getAllOperation(){
        HashMap<String, Object> hashMap = new HashMap<>();
        ArrayList<Operation> allOperation = operationService.getAllOperation();
        if(allOperation.isEmpty()){
            log.info("会员操作记录为空！");
        }
        hashMap.put("state",true);
        hashMap.put("operations",allOperation);
        hashMap.put("code",0);
        hashMap.put("msg","获取成功！");
        log.info("获取所有会员操作记录！");
        return hashMap;
    }

    @PostMapping("/getUserBytellPhone") //根据手机号查询会员
    public Map<String,Object> getUserBytellPhone(@RequestBody Map<String,Object> map){
        String tellphone = (String) map.get("tellphone");
        User user = userService.getUserByTellPhone(tellphone);  //得到会员
        HashMap<String, Object> hashMap = new HashMap<>();
        if(user != null){
            log.info("找到会员！");
            hashMap.put("state",true);
            hashMap.put("user",user);
            return hashMap;
        }
        hashMap.put("state",false);
        hashMap.put("msg","未找到该会员！");
        log.info("未找到会员！");
        return hashMap;
    }

    @PostMapping("/investCard") //充值卡金
    public Map<String,Object> investCard(@RequestBody Map<String,Object> map){
        String time = simpleDateFormat.format(new Date());
        String investCard = (String) map.get("investCard"); //充值的金额
        String tellphone = (String) map.get("tellphone");
        Map<String,Object> maps = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        User user = userService.getUserByTellPhone(tellphone);  //获取要充值的会员
        if(user == null){
            hashMap.put("state",false);
            hashMap.put("msg","充值失败！");
            log.info("充值卡金失败！");
            return hashMap;
        }
        map2.put("latest_operating_time",time);
        map2.put("tellphone",tellphone);
        int d = userService.updateUserTimeByTellPhone(map2);
        if(d==1){
            log.info("更新会员最近操作时间成功！");
        }else{
            log.info("更新会员最近操作时间失败！");
        }
        String modifyInvest = "0";
        String invest1 = user.getInvest();
        if(invest1!=null){
            modifyInvest = invest1;
        }
        String invest = Double.parseDouble(investCard) + Double.parseDouble(modifyInvest) + ""; //需要修改的总金额
        maps.put("invest",invest);
        maps.put("tellphone",tellphone);
        int i = userService.updateUserByTellPhone(maps);
        if(i == 1){
            hashMap.put("state",true);
            hashMap.put("msg","充值成功！");
            log.info("充值卡金成功！");
            int c = operationService.insertOperation(new Operation(user.getId(),user.getUsername(),user.getSex(),tellphone,
                    user.getType(), "充值卡金"+investCard,user.getRegisttime(),time));
            if(c==1){
                log.info("会员充值卡金操作记录插入！");
            }else{
                log.info("会员充值卡金操作记录插入失败！");
            }
            int f = consumeService.insertConsume(new Consume("0", investCard, time, "会员充值卡金"));
            if(f==1){
                log.info("插入一条总消费记录成功！");
            }else{
                log.info("插入一条总消费记录失败！");
            }
            int e = memberConsumeService.insertMemberConsume(new MemberConsume(user.getId(), investCard, time));
            if(e==1){
                log.info("插入一条会员消费记录成功！");
            }else{
                log.info("插入一条会员消费记录失败！");
            }
            return hashMap;
        }else{
            hashMap.put("state",false);
            hashMap.put("msg","充值卡金失败！");
            log.info("充值卡金失败！");
        }
        return hashMap;
    }

    @PostMapping("/investTimes")    //普通消费
    public Map<String,Object> investTimes(@RequestBody Map<String,Object> map){
        HashMap<String, Object> hashMap = new HashMap<>();
        String tellphone = (String) map.get("tellphone"); //电话号码
        Integer cutNum = Integer.parseInt((String)map.get("cutNum")); //剪发次数
        Integer perNum = Integer.parseInt((String)map.get("perNum")); //烫发次数
        Integer nuseNum = Integer.parseInt((String)map.get("nuseNum")); //护理次数
        Integer rootNum = Integer.parseInt((String) map.get("rootNum")); //补发根次数
        Integer dyeNum = Integer.parseInt((String)map.get("dyeNum")); //染发次数
        Integer washNum = Integer.parseInt((String)map.get("washNum")); //洗发次数
        Integer blackNum = Integer.parseInt((String)map.get("blackNum")); //黑油次数
        String invest = (String) map.get("invest"); //消费金额
        User user = userService.getUserByTellPhone(tellphone);  //查找出会员
        Map<String,Object> maps = new HashMap<>();
        maps.put("haircut",cutNum);
        maps.put("perm",perNum);
        maps.put("wash_protect",nuseNum);
        maps.put("hairroot",rootNum);
        maps.put("colour",dyeNum);
        maps.put("wash",washNum);
        maps.put("black",blackNum);
        maps.put("tellphone",tellphone);
        String time = simpleDateFormat.format(new Date());
        int i = userService.updateUserTimesByTellPhone(maps);   //更新会员次数信息
        if(i == 1){
            hashMap.put("state",true);
            hashMap.put("msg","消费成功！");
            log.info("更新会员次数信息成功！");
            //插入一条会员消费记录
            int b = memberConsumeService.insertMemberConsume(new MemberConsume(user.getId(), invest, time));
            if(b==1){
                log.info("插入一条会员消费记录！");

            }else{
                log.info("插入一条会员消费记录失败！");
            }
            int d = consumeService.insertConsume(new Consume("0", invest, time, "会员普通消费"));
            if(d==1){
                log.info("插入一条总消费记录成功！");
            }else{
                log.info("插入一条总消费记录失败！");
            }
            StringBuffer operation = new StringBuffer();    //操作内容
            int cut = cutNum-user.getHaircut();
            int per = perNum - user.getPerm();
            int nuse = nuseNum - user.getWash_protect();
            int root = rootNum - user.getHairroot();
            int colour = dyeNum - user.getColour();
            int wash = washNum - user.getWash();
            int black = blackNum - user.getBlack();
            if(cut > 0){
                operation.append("剪发次数+"+cut+" ");
            }
            if(per > 0){
                operation.append("烫发次数+"+per+" ");
            }
            if(nuse > 0){
                operation.append("护理次数+"+nuse+" ");
            }
            if(root > 0){
                operation.append("补发根次数+"+root+" ");
            }
            if(colour > 0){
                operation.append("染发次数+"+colour+" ");
            }
            if(wash > 0){
                operation.append("洗发次数+"+wash+" ");
            }
            if(black > 0){
                operation.append("黑油次数+"+black+" ");
            }
            operation.append("共消费"+invest);
            Map<String,Object> map2 = new HashMap<>();
            map2.put("latest_operating_time",time);
            map2.put("tellphone",tellphone);
            int e = userService.updateUserTimeByTellPhone(map2);
            if(e==1){
                log.info("更新会员最近操作时间成功！");
            }else{
                log.info("更新会员最近操作时间失败！");
            }
            //插入一条会员操作记录
            int c = operationService.insertOperation(new Operation(user.getId(), user.getUsername(), user.getSex(),
                    tellphone, user.getType(), operation.toString(), user.getRegisttime(), time));
            if(c==1){
                log.info("插入一条会员操作记录！");
            }else{
                log.info("插入一条会员操作记录失败！");
            }

        }else{
            hashMap.put("state",false);
            hashMap.put("msg","消费失败！");
            log.info("更新会员次数信息失败！");
        }
        return hashMap;
    }

}
